// viperconfig 用viper解析外部文件
package config

import (
	"errors"
	"github.com/spf13/viper"
	//"fmt"
	"log"
)

func NewViper(configFile string) (*viper.Viper, error) {
	if configFile == "" {
		return nil, errors.New("config file is empty, specify by --configFile=")
	}
	log.Println("read config file in", configFile)
	v := viper.New()
	v.SetConfigFile(string(configFile))
	err := v.ReadInConfig()
	if err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			// 配置文件不存在
			return nil, errors.New("config file doesn't exist")
		} else {
			return nil, err
		}
	}
	return v, nil
}
